package com.rainwood.water_authority.base;

import android.app.Application;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import com.billy.android.swipe.SmartSwipeBack;
import com.rainwood.water_authority.R;
import com.rainwood.water_authority.action.SwipeAction;
import com.rainwood.water_authority.helper.ActivityStackManager;
import com.rainwood.water_authority.http.model.RequestHandler;
import com.rainwood.water_authority.http.server.ReleaseServer;
import com.rainwood.water_authority.http.server.TestServer;
import com.rainwood.water_authority.model.CacheLoginBean;
import com.rainwood.water_authority.other.AppConfig;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.sxs.easyhttp.EasyConfig;
import com.sxs.easyhttp.config.IRequestServer;
import com.sxs.tools.sqlite.SQLiteHelper;
import com.sxs.tools.titlebar.TitleBar;
import com.sxs.tools.titlebar.style.TitleBarLightStyle;
import com.sxs.tools.toast.ToastInterceptor;
import com.sxs.tools.toast.ToastUtils;

import okhttp3.OkHttpClient;


/**
 * @author sxs
 * @date :2020/8/20
 * @description: 项目中的 Application 基类
 */
public final class App extends Application implements LifecycleOwner {

    private final LifecycleRegistry mLifecycle = new LifecycleRegistry(this);
    private static Context mContext;
    public static App app;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        mLifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        if (mContext == null) {
            mContext = getBaseContext();
        }
        initSdk(this);
        initSqlHelper();
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycle;
    }

    /**
     * 初始化一些第三方框架
     */
    public static void initSdk(Application application) {

        // 吐司工具类
        ToastUtils.init(application);

        // 设置 Toast 拦截器
        ToastUtils.setToastInterceptor(new ToastInterceptor() {
            @Override
            public boolean intercept(Toast toast, CharSequence text) {
                boolean intercept = super.intercept(toast, text);
                if (intercept) {
                    Log.e("Toast", "空 Toast");
                } else {
                    Log.i("Toast", text.toString());
                }
                return intercept;
            }
        });
        // 本地异常捕捉
        // CrashHandler.register(application);

        // 友盟统计、登录、分享 SDK
        // UmengClient.init(application);

        // Bugly 异常捕捉
        // CrashReport.initCrashReport(application, AppConfig.getBuglyId(), AppConfig.isDebug());

        // 设置全局的 Header 构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(context).setEnableLastTime(false));
        // 设置全局的 Footer 构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context).setDrawableSize(20));

        // Activity 栈管理初始化
        ActivityStackManager.getInstance().init(application);

        // 网络请求框架初始化
        IRequestServer server;
        if (AppConfig.isDebug()) {
            server = new TestServer();
        } else {
            server = new ReleaseServer();
        }

        EasyConfig.with(new OkHttpClient())
                // 是否打印日志
                .setLogEnabled(AppConfig.isDebug())
                // 设置服务器配置
                .setServer(server)
                // 设置请求处理策略
                .setHandler(new RequestHandler(application))
                // 设置请求重试次数
                .setRetryCount(3)
                // 添加全局请求参数
                // 添加全局请求头
//                .addHeader("Auth", "20191030")
                // 启用配置
                .into();

        // Activity 侧滑返回
        SmartSwipeBack.activitySlidingBack(application, activity -> {
            if (activity instanceof SwipeAction) {
                return ((SwipeAction) activity).isSwipeEnable();
            }
            return true;
        });
    }

    /**
     * 初始化本地数据库
     */
    private void initSqlHelper() {
        // 缓存登录数据
        SQLiteHelper.with(this).createTable(CacheLoginBean.class);
    }
}